package com.chilli.cosima.action.logmanager;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.chilli.cosima.action.BaseSupport;
import com.chilli.cosima.common.ErrorBean;
import com.chilli.cosima.data.bean.RunLogFileBean;
import com.chilli.cosima.utils.DateUtil;

public class RunLogQueryAction extends BaseSupport
{

	/**
	 * 注释内容
	 */
	private static final long serialVersionUID = 2969331756829944461L;

	private Log log = LogFactory.getLog(RunLogQueryAction.class);

	public String execute()
	{
		String logRoot = System.getProperty("webapp.root") + "/log/";
		File logDir = new File(logRoot);
		if (!logDir.exists() || !logDir.isDirectory())
		{
			log.info("The log file directory path(" + logDir.getAbsolutePath()
					+ ") is not exist.");
			ErrorBean bean = new ErrorBean("日志目录不存在，请检查log4j配置。",
					"runlog/runLogEditAction!jumpPage.action");
			request.setAttribute("errorBean", bean);
			return ERROR;
		}
		File[] logFiles = logDir.listFiles();
		List<RunLogFileBean> list = new ArrayList<RunLogFileBean>();
		for (int i = 0; i < logFiles.length; i++)
		{
			if (logFiles[i].isFile() && logFiles[i].exists())
			{
				RunLogFileBean bean = new RunLogFileBean();
				bean.setName(logFiles[i].getName());
				bean.setTime(DateUtil
						.formatDateTime(logFiles[i].lastModified()));
				bean.setPath(logFiles[i].getAbsolutePath());
				bean.setSize(String.valueOf(logFiles[i].length() / 1024));
				list.add(bean);
			}

		}
		request.setAttribute("valueList", list);

		return SUCCESS;
	}
}
